﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EDTS.Model.Orgnizations;
namespace EDTS.Data.Orgnizations
{
    public class GroupCommand
    {
        private static void SaveCheck(GroupModel model, DBCentre db)
        {
            if (model.Guid == Guid.Empty)
                throw new Utils.IException("无效的组编号");
            if (!string.IsNullOrEmpty(model.FullName))
                throw new Utils.IException("请提供组名称");
            if (db != null)
            {
                if (db.DataContext.Groups.Any(p => p.FullName == model.FullName && p.UnitID == model.UnitID && p.Guid != model.Guid))
                    throw new Utils.IException("组名称重复");
                if (model.Leader && db.DataContext.Groups.Any(p => p.UnitID == model.UnitID && p.Leader))
                    throw new Utils.IException("对不起，一个单位内，只能有一个领导组。");
            }
        }

        public static void CreateNew(GroupModel model)
        {
            if (model != null)
            {
                model.CreatedOn = DateTime.Now;
                model.Guid = Guid.NewGuid();
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(model, db);
                    var d = Convertor.ConvertFrom<Group, GroupModel>(model);
                    db.DataContext.Groups.Add(d);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static void Update(GroupModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Groups.FirstOrDefault(p => p.Guid == model.Guid);
                    if (d == null)
                        throw new EDTS.Utils.IException("不存在的组编号");
                    SaveCheck(model, db);
                    Convertor.UpdateValue(model, d);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static void Delete(Guid guid)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<Group>(p => p.Guid == guid);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static GroupModel GetModel(Guid guid)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Groups.FirstOrDefault(p => p.Guid == guid);
                    if (d != null)
                    {
                        var ret = Convertor.ConvertTo<GroupModel, Group>(d);
                        return ret;
                    }
                }
            }
            return null;
        }

        public static GroupCollection Gets(GroupQuery sea)
        {
            var ret = new GroupCollection();
            if (sea != null && sea.UnitGuid!=Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<Group> q = db.DataContext.Groups;
                    q = q.Where(p => p.Unit.Guid == sea.UnitGuid);
                    q = q.OrderBy(p => p.Sequence);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<GroupModel, Group, GroupCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
    }
}
